/*
Copyright (c) 2025 Huawei Technologies Co., Ltd.
This file is a part of the CANN Open Software.
Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
Please refer to the License for details. You may not use this file except in compliance with the License.
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
See LICENSE in the root of the software repository for the full text of the License. 
*/

#pragma once
#include "kernel_const.h"
#include "kernel_utils.h"
#include "kernel_operator.h"


[aicore] inline __attribute__((always_inline)) void BlockSwizzle(
    uint32_t &blockMIdx, 
    uint32_t &blockNIdx, 
    uint32_t blockIdx, 
    uint32_t mLoops, 
    uint32_t nLoops
){
    uint32_t SwizzleOffset = 5; 
    uint32_t SwizzleDirection = 0; 

    if(SwizzleDirection == 0){ // Zn
        uint32_t tileLoops = CeilDiv<uint32_t>(mLoops, SwizzleOffset); 
        uint32_t tileSize = SwizzleOffset * nLoops; 
        uint32_t tileIdx = blockIdx / tileSize; 
        uint32_t tileIdxIn = blockIdx % tileSize; 
        uint32_t tileRows = (tileIdx == tileLoops - 1 ? mLoops - tileIdx * SwizzleOffset : SwizzleOffset); 
        uint32_t tileMIdxIn = tileIdxIn % tileRows; 
        uint32_t tileNIdxIn = tileIdxIn / tileRows; 
        blockMIdx = tileIdx * SwizzleOffset + tileMIdxIn; 
        blockNIdx = (tileIdx % 2 == 0 ? tileNIdxIn : nLoops - tileNIdxIn - 1);
    }else if(SwizzleDirection == 1){ // Nz
        uint32_t tileLoops = CeilDiv<uint32_t>(nLoops, SwizzleOffset); 
        uint32_t tileSize = SwizzleOffset * mLoops; 
        uint32_t tileIdx = blockIdx / tileSize; 
        uint32_t tileIdxIn = blockIdx % tileSize; 
        uint32_t tileCols = (tileIdx == tileLoops -1 ? nLoops - tileIdx * SwizzleOffset : SwizzleOffset); 
        uint32_t tileMIdxIn = tileIdxIn / tileCols; 
        uint32_t tileNIdxIn = tileIdxIn % tileCols; 
        blockMIdx = (tileIdx % 2 == 0 ? tileMIdxIn : mLoops - tileMIdxIn - 1); 
        blockNIdx = tileIdx * SwizzleOffset + tileNIdxIn; 
    }
}
